/*
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Library General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
*/

#include "templatesmanagerdlgimpl.h"

#include <QtGui>
//
TemplatesManagerDlgImpl::TemplatesManagerDlgImpl( QWidget * parent, Qt::WFlags f) 
	: QDialog(parent, f)
{
	setupUi(this);
	
	m_currentCursor = NULL;
	
	connect(this, SIGNAL(finished(int )), this, SLOT(onFinished(int )));
}

void TemplatesManagerDlgImpl::setManager(TemplateManager *manager)
{
	m_templatesManager = manager;
	updateList();
}

void TemplatesManagerDlgImpl::setCurrentCursor(QTextCursor *textCursor)
{
	m_currentCursor = textCursor; 
}

void TemplatesManagerDlgImpl::updateList()
{
	QListWidgetItem *item;
    lwTemplates->clear();
    int count = m_templatesManager->getTemplateCount();

    for (int i = 0; i < count; i++)
    {
        item = new QListWidgetItem(m_templatesManager->getTemplateName(i), lwTemplates, 0);
        item->setData(Qt::UserRole, i);
        lwTemplates->addItem(item);
    }
}

void TemplatesManagerDlgImpl::updateUIFromModel(int index)
{
	tePreview->clear();
	tePreview->setHtml(m_templatesManager->getTemplateData(index));
	tbSelected->setChecked(m_templatesManager->isTemplateSelected(index));
}

void TemplatesManagerDlgImpl::onFinished(int result)
{
	m_templatesManager->updateModelFromList(lwTemplates);
}

void TemplatesManagerDlgImpl::on_lwTemplates_currentItemChanged(QListWidgetItem* current, QListWidgetItem* previous)
{
	if (current != NULL)
    {
        int index = current->data(Qt::UserRole).toUInt();
        updateUIFromModel(index);
        
        int listIndex = lwTemplates->currentRow();
        if (listIndex == 0)
            tbMoveUp->setEnabled(false);
        else
            tbMoveUp->setEnabled(true);

        if (listIndex == (lwTemplates->count() - 1))
            tbMoveDown->setEnabled(false);
        else
            tbMoveDown->setEnabled(true);

		tbSelected->setEnabled(true);
        tbDelete->setEnabled(true);
        tbRename->setEnabled(true);
        
        if (m_currentCursor != NULL)
        	btnInsert->setEnabled(true);
    }
}

void TemplatesManagerDlgImpl::on_tbMoveUp_clicked()
{
	int listIndex = lwTemplates->currentRow();
    if (listIndex > 0)
    {
        QListWidgetItem *item = new QListWidgetItem(*lwTemplates->currentItem());
        delete lwTemplates->currentItem();
        lwTemplates->insertItem(listIndex - 1, item);
        lwTemplates->setCurrentItem(item);
    }
}

void TemplatesManagerDlgImpl::on_tbMoveDown_clicked()
{
	int listIndex = lwTemplates->currentRow();
    if (listIndex < (lwTemplates->count() - 1))
    {
        QListWidgetItem *item = new QListWidgetItem(*lwTemplates->currentItem());
        delete lwTemplates->currentItem();
        lwTemplates->insertItem(listIndex + 1, item);
        lwTemplates->setCurrentItem(item);
    }
}

void TemplatesManagerDlgImpl::on_tbDelete_clicked()
{
	QListWidgetItem *item = lwTemplates->currentItem();
    if (item != NULL)
    {
        QMessageBox::StandardButton ret = QMessageBox::question(this, tr("Delete"), tr("Do you want to delete this template ?"),
                                          QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Cancel);

        if (ret == QMessageBox::Ok)
        {
            // Delete the item, but NOT its data in the model, as it will create an offset
            // with index which will mess up the whole thing ;)
            delete item;
        }
    }
}

void TemplatesManagerDlgImpl::on_tbRename_clicked()
{
	bool ok;
	int index = lwTemplates->currentItem()->data(Qt::UserRole).toUInt();
	QString oldName = m_templatesManager->getTemplateName(index);
	
    QString newName = QInputDialog::getText(this, tr("Rename template"),
                                            tr("New name:"), QLineEdit::Normal, oldName, &ok);
	if ((ok) &&
		(!newName.isNull()) &&
		(newName != oldName))
	{
		bool alreadyExist = m_templatesManager->doTemplateExist(newName);
		if (alreadyExist)
			QMessageBox::information(this, tr("Warning"), tr("Template already exists."), QMessageBox::Ok);
		else
		{
			m_templatesManager->setTemplateName(index, newName);
			updateList();			
		}
	}
}

void TemplatesManagerDlgImpl::on_btnInsert_clicked()
{
	if (m_currentCursor != NULL)
	{
		int index = lwTemplates->currentItem()->data(Qt::UserRole).toUInt();
		m_currentCursor->insertHtml(m_templatesManager->getTemplateData(index));
	}
}

void TemplatesManagerDlgImpl::on_tbSelected_clicked()
{
	int index = lwTemplates->currentItem()->data(Qt::UserRole).toUInt();
	m_templatesManager->setTemplateSelected(index, tbSelected->isChecked());
}
